﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPA_IN302012UV_Zyzk;
using YLW.Plat.Jhfw.Model.JhfwZyzk;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPA_IN302012UV_Zyzk
    {
        public static string TransferInfoUpdate(AddJhfwZyzkIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRPA_IN302012UV
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPA_IN302012UV" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[]
                            {
                                new item{
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = receiver_name
                                }
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[]
                            {
                                new item
                                {
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = sender_name
                                }
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "STC",
                    moodCode = "APT",
                    subject = new subject
                    {
                        typeCode = "SUBJ",
                        encounterEvent = new encounterEvent
                        {
                            classCode = "ENC",
                            moodCode = "EVN",
                            id = new id
                            {
                                item = new item[] {
                                    new item{
                                        root = "2.16.156.10011.1.12",
                                        extension = ""
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.8",
                                        extension = jsonData.series
                                    },
                                    new item{
                                        root = "2.16.156.10011.2.5.1.9",
                                        extension = jsonData.jzlsh
                                    }
                                }
                            },
                            subject = new subject
                            {
                                typeCode = "SBJ",
                                patient = new patient
                                {
                                    classCode = "PAT",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item
                                            {
                                                root = "2.16.156.10011.2.5.1.4",
                                                extension = jsonData.mrn
                                            }
                                        }
                                    }
                                }
                            },
                            location1 = new location1
                            {
                                typeCode = "DST",
                                time = new time
                                {
                                    low = new low
                                    {
                                        value = jsonData.zr_date
                                    }
                                },
                                statusCode = new statusCode(),
                                serviceDeliveryLocation = new serviceDeliveryLocation
                                {
                                    classCode = "SDLOC",
                                    location = new location
                                    {
                                        classCode = "PLC",
                                        determinerCode = "INSTANCE",
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.26",
                                                    extension = jsonData.zrksdm
                                                }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = jsonData.zrksmc
                                                }
                                            }
                                        },
                                        locatedEntityHasParts = new locatedEntityHasParts
                                        {
                                            locatedPlace = new locatedPlace[]
                                            {
                                                new locatedPlace{
                                                    Items=new object[]{
                                                        new id
                                                        {
                                                            root = "2.16.156.10011.1.27",
                                                            extension = jsonData.zrbqdm
                                                        },
                                                        new name{
                                                            item= new item
                                                            {
                                                                part = new part
                                                                {
                                                                    value = jsonData.zrbqmc
                                                                }
                                                            }
                                                        },
                                                        new locatedEntityHasParts
                                                        {
                                                            locatedPlace = new locatedPlace[]
                                                            {
                                                                new locatedPlace{
                                                                    Items=new object[]{
                                                                        new id
                                                                        {
                                                                            root = "2.16.156.10011.1.21",
                                                                            extension = jsonData.zrbf
                                                                        },
                                                                        new name{
                                                                            item= new item
                                                                            {
                                                                                part = new part
                                                                                {
                                                                                    value = jsonData.zrbf
                                                                                }
                                                                            }
                                                                        },
                                                                        new locatedEntityHasParts
                                                                        {
                                                                            locatedPlace = new locatedPlace[]
                                                                            {
                                                                                new locatedPlace{
                                                                                    Items=new object[]{
                                                                                        new id
                                                                                        {
                                                                                            root = "2.16.156.10011.1.22",
                                                                                            extension = jsonData.zrbc
                                                                                        },
                                                                                        new name{
                                                                                            item= new item
                                                                                            {
                                                                                                part = new part
                                                                                                {
                                                                                                    value = jsonData.zrbc
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            location2 = new location2
                            {
                                typeCode = "LOC",
                                time = new time
                                {
                                    low = new low
                                    {
                                        value = jsonData.zc_date
                                    }
                                },
                                statusCode = new statusCode(),
                                serviceDeliveryLocation = new serviceDeliveryLocation
                                {
                                    classCode = "SDLOC",
                                    location = new location
                                    {
                                        classCode = "PLC",
                                        determinerCode = "INSTANCE",
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item
                                                {
                                                    root = "2.16.156.10011.1.26",
                                                    extension = jsonData.zcksdm
                                                }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = jsonData.zcksmc
                                                }
                                            }
                                        },
                                        locatedEntityHasParts = new locatedEntityHasParts
                                        {
                                            locatedPlace = new locatedPlace[]
                                            {
                                                new locatedPlace{
                                                    Items=new object[]{
                                                        new id
                                                        {
                                                            root = "2.16.156.10011.1.27",
                                                            extension = jsonData.zcbqdm
                                                        },
                                                        new name{
                                                            item= new item
                                                            {
                                                                part = new part
                                                                {
                                                                    value = jsonData.zcbqmc
                                                                }
                                                            }
                                                        },
                                                        new locatedEntityHasParts
                                                        {
                                                            locatedPlace = new locatedPlace[]
                                                            {
                                                                new locatedPlace{
                                                                    Items=new object[]{
                                                                        new id
                                                                        {
                                                                            root = "2.16.156.10011.1.21",
                                                                            extension = jsonData.zcbf
                                                                        },
                                                                        new name{
                                                                            item= new item
                                                                            {
                                                                                part = new part
                                                                                {
                                                                                    value = jsonData.zcbf
                                                                                }
                                                                            }
                                                                        },
                                                                        new locatedEntityHasParts
                                                                        {
                                                                            locatedPlace = new locatedPlace[]
                                                                            {
                                                                                new locatedPlace{
                                                                                    Items=new object[]{
                                                                                        new id
                                                                                        {
                                                                                            root = "2.16.156.10011.1.22",
                                                                                            extension = jsonData.zcbc
                                                                                        },
                                                                                        new name{
                                                                                            item= new item
                                                                                            {
                                                                                                part = new part
                                                                                                {
                                                                                                    value = jsonData.zcbc
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            extension = new extension
                            {
                                dotype = new value { value1 = jsonData.dotype },
                                sjly = new value { value1 = jsonData.sjly },
                                lyxt = new value { value1 = jsonData.lyxt },
                                hoscode = new value { value1 = jsonData.hoscode },
                                zkid = new value { value1 = jsonData.zkid },
                                jzlsh = new value { value1 = jsonData.jzlsh },
                                mrn = new value { value1 = jsonData.mrn },
                                pname = new value { value1 = jsonData.pname },
                                pcid = new value { value1 = jsonData.pcid },
                                series = new value { value1 = jsonData.series },
                                stay = new value { value1 = jsonData.stay },
                                zyrq = new value { value1 = jsonData.zyrq },
                                zyysdm = new value { value1 = jsonData.zyysdm },
                                zyysxm = new value { value1 = jsonData.zyysxm },
                                zrksdm = new value { value1 = jsonData.zrksdm },
                                zrksmc = new value { value1 = jsonData.zrksmc },
                                zrbqdm = new value { value1 = jsonData.zrbqdm },
                                zrbqmc = new value { value1 = jsonData.zrbqmc },
                                zrbc = new value { value1 = jsonData.zrbc },
                                zrbf = new value { value1 = jsonData.zrbf },
                                zr_date = new value { value1 = jsonData.zr_date },
                                zcksdm = new value { value1 = jsonData.zcksdm },
                                zcksmc = new value { value1 = jsonData.zcksmc },
                                zcbqdm = new value { value1 = jsonData.zcbqdm },
                                zcbqmc = new value { value1 = jsonData.zcbqmc },
                                zcbc = new value { value1 = jsonData.zcbc },
                                zcbf = new value { value1 = jsonData.zcbf },
                                zc_date = new value { value1 = jsonData.zc_date },
                                useflag = new value { value1 = jsonData.useflag },
                                in_empid = new value { value1 = jsonData.in_empid },
                                in_date = new value { value1 = jsonData.in_date },
                                xg_empid = new value { value1 = jsonData.xg_empid },
                                xgsj = new value { value1 = jsonData.xgsj }
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
